package fair.controller;

import fair.entity.Result;
import fair.pojo.Classes;
import fair.pojo.User;
import fair.pojo.Work;
import fair.pojo.WorkClass;
import fair.service.IClassService;
import fair.service.IUserService;
import fair.service.IWorkClassService;
import fair.service.IWorkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author fair_wu
 * @version v1.0
 * @date 2021-03-06 下午 17:34
 */
@RestController
@RequestMapping("work")
@Api(tags = "作业管理")
public class WorkController {

    @Reference(version = "${dubbo.service.core.version}", group = "${dubbo.service.core.group}", timeout = 100000)
    private IWorkClassService workClassService;

    @Reference(version = "${dubbo.service.core.version}", group = "${dubbo.service.core.group}", timeout = 100000)
    private IWorkService workService;

    @Reference(version = "${dubbo.service.core.version}", group = "${dubbo.service.core.group}", timeout = 100000)
    private IUserService userService;

    @Reference(version = "${dubbo.service.core.version}", group = "${dubbo.service.core.group}", timeout = 100000)
    private IClassService classService;

    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ApiOperation("新增作业")
    @ApiImplicitParam(name = "classIds", value = "classIds", paramType = "query")
    public Result add(@RequestBody Work work, @RequestParam(value = "classIds") Integer[] classIds){
        try {
            Integer id = workService.insert(work);
            WorkClass workClass = new WorkClass();
            workClass.setWorkId(id);
            for (Integer classId : classIds) {
                workClass.setClassId(classId);
                workClassService.save(workClass);
            }
            return new Result(0, "");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, "内部异常");
        }
    }

    @ApiOperation("查询作业")
    @RequestMapping(value = "findWork", method = RequestMethod.GET)
    public Result findWork(){
        String role = null;
        try {
            Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
            GrantedAuthority authority = authorities.iterator().next();
            role = authority.getAuthority();
            String name = SecurityContextHolder.getContext().getAuthentication().getName();
            String t = "teacher";
            Map<String, Object> map = new HashMap<>(10);
            List<Work> workList = new ArrayList<>();
            if (t.equals(role)){
                workList = workService.findWorks(name);
            }else {
                User user = userService.findOne(name);
                String classname = user.getClassname();
                Classes classes = classService.findByName(classname);
                Integer id = classes.getId();
                List<WorkClass> lists = workClassService.findByClassId(id);
                for (WorkClass workClass : lists) {
                    Integer workId = workClass.getWorkId();
                    workList.add(workService.getById(workId));
                }
            }
            return new Result(0, "", workList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(0, "", role);
    }
}
